package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.commom.R;
import com.itheima.ydd.dto.SysRoleDto;
import com.itheima.ydd.entity.SysRole;
import com.itheima.ydd.entity.SysRoleMenu;
import com.itheima.ydd.service.SysRoleMenuService;
import com.itheima.ydd.service.SysRoleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("sys_role")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    /**
     * 分页查询获取当前角色信息
     * @param name
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public R getRoleInfo(String name , Integer page , Integer limit){
        Page<SysRole> pageInfo = new Page<>(page , limit);
        LambdaQueryWrapper<SysRole> roleWrapper = new LambdaQueryWrapper<>();
        //在传入条件存在才查询
        roleWrapper.like(StringUtils.isNotEmpty(name) , SysRole::getName ,name);
        sysRoleService.page(pageInfo,roleWrapper);
        Map<String , Object> map = new HashMap<>();
        //设置基本数据
        map.put("count",(int)pageInfo.getTotal());
        map.put("totalPages", (int)pageInfo.getPages());
        map.put("pageSize", (int)pageInfo.getSize());
        map.put("currentPage",(int)pageInfo.getCurrent());

        //设置分页详情数据
        List<SysRole> roles = pageInfo.getRecords();

        List<SysRoleDto> roleDtos = new ArrayList<>();
        //拷贝基本信息
        for (SysRole role : roles) {
            SysRoleDto roleDto = new SysRoleDto();
            BeanUtils.copyProperties(role,roleDto);
            roleDtos.add(roleDto);
        }

        //添加详细信息
        roleDtos = roleDtos.stream().map(item ->{
            item.setCreate_date(item.getCreateDate());
            item.setUpdate_date(item.getUpdateDate());
            item.setDel_flag(item.getDelFlag());
            item.setRole_id(item.getId());
            LambdaQueryWrapper<SysRoleMenu> menuWrapper = new LambdaQueryWrapper<>();
            menuWrapper.eq(SysRoleMenu::getRoleId,item.getId());
            menuWrapper.select(SysRoleMenu::getMenuId);
            List<SysRoleMenu> list = sysRoleMenuService.list(menuWrapper);
            List<String> roleMenus = new ArrayList<>();
            for (SysRoleMenu sysRoleMenu : list) {
                roleMenus.add(sysRoleMenu.getMenuId());
            }
            item.setRole_menus(roleMenus);
            return item;
        }).collect(Collectors.toList());

        map.put("data" , roleDtos);
        return R.success("查询成功", map);
    }


    /**系统设置-用户管理  获取所有的role
     * sys_role/allData
     * @return
     */
    @GetMapping("/allData")
    public R getAllData(){
        LambdaQueryWrapper<SysRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysRole::getDelFlag,0);
        wrapper.orderByAsc(SysRole::getCreateDate);
        List<SysRole> sysRoles = sysRoleService.list(wrapper);
        return R.success("",sysRoles);
    }



    /**
     * 添加角色信息 ， 并添加对应的菜单关系
     * @param sysRoleDto
     * @return
     */
    @Transactional
    @PostMapping("/add")
    public R addRole(@RequestBody SysRoleDto sysRoleDto){
        //添加基本信息到角色表
        sysRoleDto.setDelFlag("0");
        sysRoleDto.setSuperadmin(0);
        sysRoleDto.setDefaultData("1");
        sysRoleService.save(sysRoleDto);
        //添加后添加
        //添加对应的角色编号和对应菜单编号的关系
        String id = sysRoleDto.getId();
        List<String> role_menus = sysRoleDto.getRole_menus();
        for (String role_menu : role_menus) {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();
            sysRoleMenu.setRoleId(id);
            sysRoleMenu.setMenuId(role_menu);
            sysRoleMenuService.save(sysRoleMenu);
        }

        return R.success("添加成功",null);
    }

    /**
     * 修改用户时的数据回显
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public R getRoleInfo(String id){
        //根据id查询对应的角色信息，并返回至前端
        SysRoleDto sysRoleDto = sysRoleService.getInfo(id);

        return R.success("",sysRoleDto);
    }


    /**
     * 修改角色信息
     * @param sysRoleDto
     * @return
     */
    @PostMapping("/update")
    public R updateRole(@RequestBody SysRoleDto sysRoleDto){
        sysRoleService.updateRole(sysRoleDto);
        return R.success("修改成功" ,null);
    }

    /**
     * 删除对应的角色信息
     * @param id
     * @return
     */
    @GetMapping("/del")
    public R deleteRole(String id){
        sysRoleService.del(id);
        return R.success("删除成功",null);
    }
}
